package com.hellogood.yaohuo.client.setting;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.shared.entity.Group;
import com.hellogood.yaohuo.shared.services.GroupService;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;
import com.smartgwt.client.widgets.layout.VLayout;

public class GroupModifyWindow extends Window {

	private TextItem codeItem = null;
	private TextItem nameItem = null;
	private Button saveBtn = null;
	private Label msg = null;
	private DynamicForm form = null;
	private GroupController gc;

	public GroupModifyWindow(GroupController gc) {
		this.gc = gc;
		initView();		
	}

	private void initView() {
		LengthRangeValidator length4Validator = new LengthRangeValidator();
		length4Validator.setMin(1);
		length4Validator.setMax(4);
		length4Validator.setErrorMessage(CommonMessages.MaxLength4);

		LengthRangeValidator length10Validator = new LengthRangeValidator();
		length10Validator.setMin(1);
		length10Validator.setMax(10);
		length10Validator.setErrorMessage(CommonMessages.MaxLength10);

		codeItem = new TextItem("code", "用户组编码");
		codeItem.setRequired(true);
		codeItem.setValidators(length4Validator);

		nameItem = new TextItem("name", "用户组名称");
		nameItem.setRequired(true);
		nameItem.setValidators(length10Validator);

		form = new DynamicForm();
		form.setSize("250", "80");
		form.setFields(codeItem, nameItem);

		saveBtn = new Button("保存");
		saveBtn.addClickHandler(new SaveBtnClickHandler());

		msg = new Label();
		msg.setSize("250", "30");

		VLayout layout = new VLayout();
		layout.addMember(form);
		layout.addMember(saveBtn);
		layout.addMember(msg);
		layout.setTop(50);
		layout.setLeft(10);
		addChild(layout);
	}

	private class SaveBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			if (form.validate()) {
				Object code = codeItem.getValue();
				Object name = nameItem.getValue();

				Group g = new Group();
				if (code != null && name != null) {
					g.setGroupCode(code.toString());
					g.setGroupName(name.toString());
					GroupService.Util.getInstance().createGroup(g, new AsyncCallback<String>() {
						@Override
						public void onSuccess(String result) {
							if (result.equals("")) {
								gc.initData();
								msg.setContents(CommonMessages.CreateGroupSuccess);
								codeItem.setValue("");
								nameItem.setValue("");
								saveBtn.setTitle(CommonMessages.CreateAgain);
							} else {
								msg.setContents(result);
							}
						}

						@Override
						public void onFailure(Throwable caught) {
							msg.setContents(caught.getMessage());
						}
					});

				} else {
					msg.setContents(CommonMessages.CodeNameInputError);
				}
			}
		}
	}
}
